import 'package:intl/intl.dart';

class TimeUtil {
  static DateFormat _dataFormat = DateFormat('yyyy-MM-dd HH:mm:ss');
  static DateFormat _sendFormat = DateFormat('HH:mm');
  static DateFormat _timeFormat = DateFormat('HH:mm_M月d日');

  ///2020年8月16日 17:00-18:00
  static String getStringTime(String startTime, String endTime) {
    String time;
    if (startTime != null && startTime.trim() != '') {
      DateTime dStartTime = _dataFormat.parse(startTime);
      DateTime dEndTime = _dataFormat.parse(endTime);
      time =
          '${dStartTime.year}年${dStartTime.month}月${dStartTime.day}日 ${_sendFormat.format(dStartTime)}-${_sendFormat.format(dEndTime)}';
    } else {
      time = '无';
    }
    return time;
  }

  static String getStringTime2(String startTime, String endTime) {
    StringBuffer time = StringBuffer();
    if (startTime != null && startTime.trim() != '') {
      DateTime dStartTime = _dataFormat.parse(startTime);
      DateTime dEndTime = _dataFormat.parse(endTime);
      DateTime nowTime = DateTime.now();
      var data = dStartTime.difference(nowTime);
      int day = data.inDays;
      if (day > 0) {
        //时间未到
        if (day == 1) {
          time.write('明天');
        } else if (day == 2) {
          time.write('后天');
        } else {
          time.write('$day天后');
        }
      } else if (day == 0) {
        //今日
        time.write('今天');
      } else {
        //超时
        if (day == -1) {
          time.write('昨天');
        } else if (day == -2) {
          time.write('前天');
        } else {
          time.write("${day.abs()}天前");
        }
      }
      time.write(' ${_sendFormat.format(dStartTime)}-${_sendFormat.format(dEndTime)}');
    }else{
      time.write('无');
    }
    return time.toString();
  }

  static String getStateTime(String time) {
    if (time != null && time.trim() != '') {
      return _timeFormat.format(_dataFormat.parse(time));
    } else {
      return '未知时间';
    }
  }
}
